package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}


	@Operation(summary = "根据声音id进行数据回显")
	@GetMapping("/getTrackInfo/{id}")
	public Result getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "删除声音")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	@Operation(summary = "查询声音列表")
	@PostMapping("/findUserTrackPage/{page}/{limit}")
	public Result findUserTrackPage(@PathVariable Long page,
	                                @PathVariable Long limit,
									@RequestBody TrackInfoQuery trackInfoQuery) {
		Long userId= null==AuthContextHolder.getUserId()?1L:AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		Page<TrackListVo> trackListVoPage=new Page<>(page,limit);
		trackListVoPage=trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		return Result.ok(trackListVoPage);
	}


	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		Long userId= null==AuthContextHolder.getUserId()?1L:AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(userId,trackInfoVo);
		return Result.ok();
	}

	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(@RequestBody MultipartFile file) {
		Map<String,Object> map =trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}


}

